package com.dzf.controller;

import com.dzf.entity.RespJson;
import com.dzf.entity.User;
import com.dzf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * @author DZF
 * 后台的用户管理模块
 */
@RestController
@RequestMapping("/api/adm/user")
public class AdmUserController {
    @Autowired
    UserService userService;


    /**
     * 添加管理员
     * @param user 用户信息
     * @param pic  头像信息
     * @return
     */
    @RequestMapping("/addAdm")
    public RespJson addAdm(User user,@RequestParam(value = "pic",required = false) MultipartFile pic){

        userService.addAdm(user,pic);
        return RespJson.pack(200,"添加管理员成功....",null);
    }

    /**
     * 查询注册用户
     * @param start 起始页
     * @param num   每页行数
     * @return 进行分页后的用户信息
     */
    @RequestMapping("findAll/{start}/{num}")
    public RespJson findAll(@PathVariable("start")int start,@PathVariable("num")int num)
    {
        Map<String, Object> map = new HashMap<>();
        map.put("start", (start*num)-num);
        map.put("num",num);
        return RespJson.pack(200,"OK",userService.findAll(map));
    }

    /**
     * 密码重置
     * @param uid 用户ID
     * @return 无data
     */
    @RequestMapping("/resetPwd/{uid}")
    public RespJson resetPwd(@PathVariable("uid")int uid)
    {
        userService.resetPwd(uid);
        return RespJson.pack(200,"密码已重置为111111",null);
    }

    /**
     * 冻结用户
     * @param uid 用户ID
     * @return 无data
     */
    @RequestMapping("close/{uid}")
    public RespJson close(@PathVariable("uid")int uid){
        userService.close(uid);
        return RespJson.pack(200,"用户已被冻结",null);
    }

    /**
     * 查询某一个用户
     * @param uid 用户ID
     * @return
     */
    @RequestMapping("findById/{uid}")
    public RespJson findById(@PathVariable("uid")int uid){
        return RespJson.pack(200,"",userService.findById(uid));
    }

}
